package com.enjoy.mjbaseframe.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import static com.enjoy.mjbaseframe.constant.BaseConstant.SP_FILE_NAME;

/**
 * SharedPreferences工具类
 * Created by Administrator on 2017/11/30.
 */

public class SPUtil {

    private static SharedPreferences PREFERENCES = AppUtil.getApp().getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
    private static SharedPreferences.Editor editor = PREFERENCES.edit();

    /**
     * 保存数据的方法，拿到数据保存数据的基本类型，然后根据类型调用不同的保存方法
     *
     * @param key
     * @param object
     */
    public static boolean put(String key, Object object) {
        if (object instanceof String) {
            editor.putString(key, (String) object);
        } else if (object instanceof Integer) {
            editor.putInt(key, (Integer) object);
        } else if (object instanceof Boolean) {
            editor.putBoolean(key, (Boolean) object);
        } else if (object instanceof Float) {
            editor.putFloat(key, (Float) object);
        } else if (object instanceof Long) {
            editor.putLong(key, (Long) object);
        } else {
            editor.putString(key, object.toString());
        }
        return editor.commit();
    }

    /**
     * 获取保存数据的方法，我们根据默认值的到保存的数据的具体类型，然后调用相对于的方法获取值
     *
     * @param key           键的值
     * @param defaultObject 默认值
     * @return
     */

    public static Object get(String key, Object defaultObject) {
        SharedPreferences.Editor editor = PREFERENCES.edit();
        if (defaultObject instanceof String) {
            return PREFERENCES.getString(key, (String) defaultObject);
        } else if (defaultObject instanceof Integer) {
            return PREFERENCES.getInt(key, (Integer) defaultObject);
        } else if (defaultObject instanceof Boolean) {
            return PREFERENCES.getBoolean(key, (Boolean) defaultObject);
        } else if (defaultObject instanceof Float) {
            return PREFERENCES.getFloat(key, (Float) defaultObject);
        } else if (defaultObject instanceof Long) {
            return PREFERENCES.getLong(key, (Long) defaultObject);
        } else {
            return PREFERENCES.getString(key, null);
        }

    }

    /**
     * 保存对象到SP，对象需要序列化
     * @param object
     * @param key
     * @return
     */
    public static boolean setObjectToSP(Object object, String key) {
        if (object == null) {
            return remove(key);
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        // 将对象放到OutputStream中
        // 将对象转换成byte数组，并将其进行base64编码
        String objectStr = new String(Base64.encode(baos.toByteArray(),
                Base64.DEFAULT));
        try {
            baos.close();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return put(key, objectStr);
    }

    /**
     * 从SP中获取缓存对象
     * @param key
     * @return
     */
    public static Object getObjectFromSP(String key) {
        try {
            String wordBase64 = PREFERENCES.getString(key, "");
            // 将base64格式字符串还原成byte数组
            if (wordBase64 == null || wordBase64.equals("")) { // 不可少，否则在下面会报java.io.StreamCorruptedException
                return null;
            }
            byte[] objBytes = Base64.decode(wordBase64.getBytes(),
                    Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(objBytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            // 将byte数组转换成product对象
            Object obj = ois.readObject();
            bais.close();
            ois.close();
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除SP里指定key对应的数据项
     * @param key
     */
    public static boolean remove(String key) {
        editor.remove(key);
        return editor.commit();
    }

    /**
     * 清空SP里所以数据
     */
    public static boolean clear() {
        editor.clear();
        return editor.commit();
    }

    /**
     * 判断SP是否包含特定key的数据
     * @param key
     * @return
     */
    public boolean contains(String key){
        return PREFERENCES.contains(key);
    }
}
